<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta http-equiv="x-UA-Compatible" content="ie=Edge">
   	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Crafty - 文本</title>
	<link rel="shortcut icon" href="../favicon.ico">
	<link type="text/css" rel="stylesheet" href="../craftyjs-site.css" />
</head>
<body>
	<div id = "header-background"> </div>
	<div id = "page-frame">
	<div id="header">
		<nav class="navbar">
  			<div class="container-fluid">
  				<div class="navbar-header">
  					<a class="navbar-brand" href="../"> <img class="logo" src="../images/text-logo.png" /> </a>
  					
				</div>
				<div class="collapse navbar-collapse navbar-right" id="bs-example-navbar-collapse-1">
					<ul class="nav navbar-nav">
						<li><a href="../">首页</a></li>
						<li><a href="../getting-started/">快速开始</a></li>
						<li><a href="../documentation/">文档</a></li>
						<li><a href="../api/">API</a></li>
						<li><a href="../components/">组件</a></li>
						<li class="emph"><a href="../#install">下载</a></li>
					</ul>
				</div>
			</div>
		</nav>
	</div>


	<div id="main">
		<div id="content" class="container">
			
<div id="docs">
	<div id='doc-nav'>
		<ul id='doc-level-one'>
			<li>
				Topics
				<ul>
					<li>
						<a href='../documentation/'>
							文档
						</a>
					</li>
					<li>
						<a href='../documentation/entities.html'>
							实体
						</a>
					</li>
					<li>
						<a href='../documentation/events.html'>
							事件
						</a>
					</li>
					<li>
						<a href='../documentation/components.html'>
							组件
						</a>
					</li>
					<li>
						<a href='../documentation/2d.html'>
							2D绘图
						</a>
					</li>
					<li>
						<a href='../documentation/text.html'>
							文本
						</a>
					</li>
					<li>
						<a href='../documentation/keyboard.html'>
							键盘
						</a>
					</li>
					<li>
						<a href='../documentation/mouse.html'>
							鼠标
						</a>
					</li>
					<li>
						<a href='../documentation/sound.html'>
							声音
						</a>
					</li>
					<li>
						<a href='../documentation/sprites.html'>
							精灵
						</a>
					</li>
					<li>
						<a href='../documentation/scenes.html'>
							场景
						</a>
					</li>
					<li>
						<a href='../documentation/gameloop.html'>
							游戏循环
						</a>
					</li>
				</ul>
			</li>
		</ul>
	</div>
	<div id='doc-content' class="markdown">
		

		<h2> Text </h2>
<p>It is very easy to add text to your game. Create an entity
which contains the component <a href="../api/Text.html">&quot;Text&quot;</a>:</p>
<pre><code><span class="hljs-keyword">var</span> helloWorldText = Crafty.e(<span class="hljs-string">'2D, DOM, Text'</span>)
  .attr({
    <span class="hljs-attr">x</span>: <span class="hljs-number">10</span>,
    <span class="hljs-attr">y</span>: <span class="hljs-number">10</span>
  });</code></pre>
<p>A very simple text example looks like this:</p>
<pre><code>helloWorldText.text(<span class="hljs-string">'Hello World'</span>);</code></pre>
<h2 id="styling-text">Styling text</h2>
<p>The &quot;Text&quot; component provides several methods for styling the text.</p>
<p>You can set the color by using the <code>.textColor()</code> method:</p>
<pre><code>helloWorldText.textColor(<span class="hljs-string">'red'</span>);</code></pre>
<p>You can customize the text further by passing an object of styles to the ´.textFont()` method:</p>
<pre><code>helloWorldText.textFont({
  <span class="hljs-attr">size</span>: <span class="hljs-string">'20px'</span>,
  <span class="hljs-attr">weight</span>: <span class="hljs-string">'bold'</span>
});</code></pre>
<p>Possible keys are:</p>
<ul>
<li>type</li>
<li>weight</li>
<li>size</li>
<li>family</li>
<li>lineHeight</li>
<li>variant</li>
</ul>
<h2 id="2d-properties">2D properties</h2>
<p>Most of the standard <a href="../api/2D.html">2D</a> properties and methods will function with text: you can move it, rotate it, attach it to follow other entities, and so on.  There are a couple of caveats:</p>
<ul>
<li>Text will work with <a href="../api/Canvas.html">Canvas</a> and <a href="../api/DOM.html">DOM</a> layers.   However, styling will work best with the DOM layer, since browsers are extremely good at displaying text!</li>
<li>The height and width of the text entity will be set automatically when using Canvas, since the html canvas has no facilities for reflowing text.</li>
</ul>
	</div>
</div>
			
		</div>
	</div>
	<div class="clearer"></div>
	<div id="footer">
		<div id="contact">
			<a href="https://groups.google.com/forum/#!forum/craftyjs">
				<img src="../images/google.png" />
				google groups</a>
			<a href="mailto:starwed@gmail.com">
				<img src="../images/email.png" />
				starwed@gmail.com</a>
			<a href="https://github.com/craftyjs/Crafty">
				<img src="../images/github.png" />
				github</a>
		</div>
		<ul>
			<li><a href="../">首页</a></li>
				<li><a href="../getting-started/">快速开始</a></li>
				<li><a href="../documentation/">文档</a></li>
				<li><a href="../api/">API</a></li>
				<li><a href="../components/">组件</a></li>
				<li class="emph"><a href="../#install">下载</a></li>
		</ul>
		<p>
			&copy; Crafty 2010-2015. Crafty is distributed under the <a href="http://en.wikipedia.org/wiki/MIT_License">MIT License</a>.
		</p>
	</div>
</div>
</body>
</html>
